<template>
  <div class="channel-edit">
     <van-cell :border="false" title="单元格" value="内容" >
       <template #default>
         <van-button @click="isedit=!isedit" round class="edit-btn"  size="small" color="red" plain type="primary">{{isedit?'完成':'编辑'}}</van-button>
       </template>
     </van-cell>
      <!-- 中间格子内容 -->
      <van-grid class="mygrid" :border="false" :gutter="10">
        <!-- <van-grid-item :class="isedit?'active':''" text="文字" :icon="isedit?'close':''"/> -->
        <van-grid-item @click="editchange(index,obj.id)" :class="{active:active===index}" v-for="(obj,index) in channels" :key="obj.id" :text="obj.name" :icon="isedit&&index!=0?'close':''"/>
      </van-grid>
      <!-- 添加 -->
       <van-cell :border="false" title="单元格" value="内容" >
       <template #default>
         <van-button round class="edit-btn"  size="small" color="red" plain type="primary">编辑</van-button>
       </template>
     </van-cell>
      <van-grid direction="horizontal" icon-size="14px" class="mygrid" :border="false" :gutter="10">
        <van-grid-item @click="addchannel(obj)" icon="plus" :text="obj.name" v-for="obj in recommendchannels" :key="obj.id" />

      </van-grid>
  </div>
</template>

<script>
import { getAllchannels, addchannels, deltechannels } from '@/api/getverify'
import { mapState } from 'vuex'
import { setStorage } from '@/utils/storage'
import { Toast } from 'vant'
export default {
  name: 'channel',
  data () {
    return {
      isedit: true,
      channelsall: []
    }
  },
  props: {
    channels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  created () {
    this.getalls()
  },
  computed: {
    ...mapState(['user']),
    recommendchannels () {
      return this.channelsall.filter(item => {
        return !this.channels.some(item2 => item2.id === item.id)
      })
    }
  },
  methods: {
    editchange (index, id) {
      if (this.isedit) {
        if (index === 0) {
          console.log('跳转')
          return
        }
        this.channels.splice(index, 1)
        // this.$emit('deletchangeactive')
        if (index < this.active) {
          this.$emit('deletchangeactive')
        }
        this.deleuserchannel(id)
      } else {
        this.$emit('ongo', index)
      }
    },
    async getalls () {
      const res = await getAllchannels()
      this.channelsall = res.channels

      // this.channelsall.filter(item => {
      //   const flag = this.channels.some(item1 => item1.id === item.id)
      //   return !flag
      // })
    },
    async addchannel (obj) {
      this.channels.push(obj)
      Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      if (this.user) {
        try {
          await addchannels({
            id: obj.id,
            seq: this.channels.length
          })
          Toast.success('添加成功')
        } catch (error) {
          Toast.fail('添加失败')
        }
      } else {
        setStorage('MY-CHANNEL', this.channels)
        Toast.success('添加成功')
      }
    },
    async deleuserchannel (id) {
      if (!this.user) return setStorage('MY-CHANNEL', this.channels)
      try {
        Toast.loading({
          message: '加载中...',
          forbidClick: true,
          duration: 0
        })
        const res = await deltechannels(id)
        console.log(res)
        Toast.success('删除成功')
      } catch (error) {
        Toast.success('删除失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.channel-edit {
  .edit-btn {
    width: 104px;
    height: 48px;
  }
  /deep/.mygrid {
    .van-grid-item{
      width: 160px;
      height: 86px;
      border-radius: 6px;
      .van-grid-item__content--center{
        background: #F4F5F6;
      }
      .van-icon-close {
      font-size: 35px;
      position: absolute;
      top: -10.5px;
      right: 5px;
    }
    .van-grid-item__icon+.van-grid-item__text {
      margin-top: 0;
    }

    }
    .active{
      .van-grid-item__content{
      .van-grid-item__text{
        color: red !important;
      }
    }
    }

  }
}

</style>
